Following tutorials show how to create Class that implements CommandLineRunner to run Task at App startup.
You simply need to @Override its run() Method with your custom code.
You can use @Order(1) to specify order in which such Classes (and their run() Methods) should be executed.
Application Schema [Results]
Create Project: springboot_commandlinerunner (Spring Boot Starters are not used)
Create Package: runners (inside main package)
– Create Class: MyRunner1.java (inside runners package)
– Create Class: MyRunner2.java (inside runners package)
MyRunner1.java
package com.ivoronline.springboot_commandlinerunner.runners;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(1)
@Component
public class MyRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Hello from MyRunner1");
}
}
MyRunner2.java
package com.ivoronline.springboot_commandlinerunner.runners;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(2)
@Component
public class MyRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Hello from MyRunner2");
}
}